package com.future.mayi.qsale.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.future.baselib.utils.LogUtils;
import com.future.mayi.R;
import com.future.mayi.adapter.BaseRecyclerViewAdapter;
import com.future.mayi.model.interfaces.OnItemClickListener;
import com.future.mayi.qsale.dialog.SalePhotoSampleDialog;
import com.future.mayi.qsale.model.SubscribeSaleModel;
import com.future.mayi.qsale.view.SelectImgView;
import com.future.mayi.utils.Arith;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/6/15.
 */

public class SubscribeSaleAdapter extends BaseRecyclerViewAdapter<SubscribeSaleModel> {

    public static final int TYPE_HAVE_EDITED = 1;
    public static final int TYPE_BEEN_EDITED = 2;
    public static final int TYPE_ADD_ITEM = 3;

    private OnItemClickListener mOnSelectListener;

    private int maxSelectNum = 12;

    public void setOnSelectListener(OnItemClickListener onItemClickListener) {
        this.mOnSelectListener = onItemClickListener;
    }

    public SubscribeSaleAdapter(Context context, List<SubscribeSaleModel> list) {
        super(context, list);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_HAVE_EDITED) {
            return new HaveEditedHolder(getView(parent, R.layout.item_sale_have_edited));
        } else if (viewType == TYPE_BEEN_EDITED) {
            return new BeenEditedHolder(getView(parent, R.layout.item_sale_been_edited));
        } else {
            return new AddItemHolder(getView(parent, R.layout.item_sale_add));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof AddItemHolder) {
            bindAddItemHolder((AddItemHolder) holder, position);
        }
        if (position == mList.size()) return;
        SubscribeSaleModel subscribeSaleModel = mList.get(position);
        if (subscribeSaleModel == null) return;
        if (holder instanceof BeenEditedHolder) {
            bindBeenEditedView((BeenEditedHolder) holder, subscribeSaleModel, position);
        } else if (holder instanceof HaveEditedHolder) {
            bindHaveEditedView((HaveEditedHolder) holder, subscribeSaleModel, position);
        }
    }

    private void bindAddItemHolder(AddItemHolder holder, int position) {
        holder.tvAddItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtils.d(TAG, "onClick");
                addItem();
            }
        });
    }

    private void bindHaveEditedView(HaveEditedHolder holder, SubscribeSaleModel subscribeSaleModel, int position) {

        if (subscribeSaleModel != null && subscribeSaleModel.getSelectImgs() != null && subscribeSaleModel.getSelectImgs().size() > 0) {
            Glide.with(mContext).load(subscribeSaleModel.getSelectImgs().get(0)).placeholder(R.color.black4).into(holder.ivImage);
        }
        holder.tvPrice.setText(Arith.getFormatPrice(subscribeSaleModel.getExpectPrice()));
        holder.tvTitle.setText(subscribeSaleModel.getBrand() + "-" + subscribeSaleModel.getSize());
    }

    private BeenEditedHolder recentEditedHolder;

    /**
     * 添加一个
     */
    private void addItem() {
        int position = getRecentEditPosition();
        LogUtils.d(TAG, "position:" + position);
        if (position == -1) return;
        //保存正在编辑的item
        if (recentEditedHolder.getItemViewType() == TYPE_BEEN_EDITED) {
            if (!TextUtils.isEmpty(recentEditedHolder.tvBrand.getText().toString())) {
                mList.get(position).setBrand(recentEditedHolder.tvBrand.getText().toString());
            }

            if (!TextUtils.isEmpty(recentEditedHolder.tvStyle.getText().toString())) {
                mList.get(position).setStyle(recentEditedHolder.tvStyle.getText().toString());
            }

            if (!TextUtils.isEmpty(recentEditedHolder.edtPrice.getText().toString())) {
                mList.get(position).setExpectPrice(Double.valueOf(recentEditedHolder.edtPrice.getText().toString()));
            }

            if (TextUtils.isEmpty(recentEditedHolder.edtDesc.getText().toString())) {
                mList.get(position).setDesc(recentEditedHolder.edtDesc.getText().toString());
            }

            if (TextUtils.isEmpty(recentEditedHolder.edtSize.getText().toString())) {
                mList.get(position).setSize(recentEditedHolder.edtSize.getText().toString());
            }

        }
        mList.get(position).setShowType(SubscribeSaleModel.TYPE_HAVE_EDITED);

        SubscribeSaleModel subscribeSaleModel = new SubscribeSaleModel();
        subscribeSaleModel.setShowType(SubscribeSaleModel.TYPE_BEEN_EDIT);

        addMore(subscribeSaleModel);
    }

    private void deleteItem(int position) {
        if (position - 1 < 0) return;
        delete(position);
        mList.get(position - 1).setShowType(SubscribeSaleModel.TYPE_BEEN_EDIT);
        notifyDataSetChanged();
    }

    public int getRecentEditPosition() {
        int position = -1;
        for (int i = 0; i < mList.size(); i++) {
            if (mList.get(i) != null && mList.get(i).getShowType() == SubscribeSaleModel.TYPE_BEEN_EDIT) {
                position = i;
                break;
            }
        }
        return position;
    }

    public void addImg(List<String> imgs) {
        List<String> mListImgs = null;
        int recentEditItem = -1;

        for (int i = 0; i < mList.size(); i++) {
            if (mList.get(i).getShowType() == SubscribeSaleModel.TYPE_BEEN_EDIT) {
                mListImgs = mList.get(i).getSelectImgs();
                recentEditItem = i;
                break;
            }
        }

        if (recentEditItem == -1) return;

        if (mListImgs == null) return;
        if (mListImgs != null && mListImgs.size() > 0) {
            mListImgs.remove(mListImgs.size() - 1);
        }

        if (mListImgs.size() != maxSelectNum && mListImgs.size() + imgs.size() < maxSelectNum) {
            imgs.add(SelectImgAdapter.ADD_ITEM_STRING);
        }

        if (mListImgs.size() == maxSelectNum) {
            mList.get(recentEditItem).setSelectImgs(imgs);
            notifyDataSetChanged();
        } else {
            mListImgs.addAll(imgs);
            mList.get(recentEditItem).setSelectImgs(mListImgs);
            notifyDataSetChanged();
        }

    }

    private static final String TAG = "SubscribeSaleAdapter";

    private void bindBeenEditedView(BeenEditedHolder beenEditedHolder, SubscribeSaleModel subscribeSaleModel, final int position) {

        if (position == 0) {
            beenEditedHolder.ivDelete.setVisibility(View.GONE);
        } else {
            beenEditedHolder.ivDelete.setVisibility(View.VISIBLE);
            beenEditedHolder.ivDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    deleteItem(position);
                }
            });
        }

        this.recentEditedHolder = beenEditedHolder;
        beenEditedHolder.selectImgView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void OnItem(View view, int position) {
                if (mOnSelectListener != null) {
                    mOnSelectListener.OnItem(view, position);
                }
            }

            @Override
            public void OnLongItem(View view, int position) {

            }
        });
        beenEditedHolder.selectImgView.setImgList(subscribeSaleModel.getSelectImgs());

        beenEditedHolder.tvPhotoSample.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SalePhotoSampleDialog salePhotoSampleDialog = new SalePhotoSampleDialog(mContext);
                salePhotoSampleDialog.show();
            }
        });

    }

    @Override
    public int getItemViewType(int position) {
        if (position == mList.size()) {
            return TYPE_ADD_ITEM;
        } else if (mList.get(position).isBeenEdited()) {
            return TYPE_BEEN_EDITED;
        } else {
            return TYPE_HAVE_EDITED;
        }
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size() + 1;
    }

    public static class BeenEditedHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_delete)
        ImageView ivDelete;
        @BindView(R.id.select_image_view)
        SelectImgView selectImgView;
        @BindView(R.id.tv_hint)
        TextView tvHint;
        @BindView(R.id.iv_brand_arrow)
        ImageView ivBrandArrow;
        @BindView(R.id.tv_brand)
        TextView tvBrand;
        @BindView(R.id.rl_brand)
        RelativeLayout rlBrand;
        @BindView(R.id.iv_style_arrow)
        ImageView ivStyleArrow;
        @BindView(R.id.tv_style)
        TextView tvStyle;
        @BindView(R.id.rl_style)
        RelativeLayout rlStyle;
        @BindView(R.id.rl_price)
        RelativeLayout rlPrice;
        @BindView(R.id.textView)
        TextView textView;
        @BindView(R.id.rl_size)
        RelativeLayout rlSize;
        @BindView(R.id.rl_desc)
        RelativeLayout rlDesc;
        @BindView(R.id.tv_text_num)
        TextView tvTextNum;
        @BindView(R.id.edt_desc)
        EditText edtDesc;
        @BindView(R.id.edt_price)
        EditText edtPrice;
        @BindView(R.id.edt_size)
        EditText edtSize;
        @BindView(R.id.tv_photo_sample)
        TextView tvPhotoSample;


        public BeenEditedHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public static class HaveEditedHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_image)
        ImageView ivImage;
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.tv_hint_want_price)
        TextView tvHintWantPrice;
        @BindView(R.id.tv_hint_dollor)
        TextView tvHintDollor;
        @BindView(R.id.tv_price)
        TextView tvPrice;

        public HaveEditedHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public static class AddItemHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_add_item)
        TextView tvAddItem;

        public AddItemHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
